
var listName = "list";
var listType = 1; 

function clearList(){
	
  	$("select[id^='"+listName+"']").each(clearChildList);
  	
}
function clearChildList(i, input){
  	if(i > (listType-1)){
  		$(input).remove();
  	}
}

function refreshArea(string){

	var id=$("#"+listName+"_"+string).val();
	
	if(id=="" || id==0){ 
		if(string=='pro'){		
  			listType = 1;
  			$("#level").val(0);
  		}else if(string=='city'){
  			listType = 2;
  			$("#level").val(1);
  		}else if(string=='is_ring'){
  			listType = 3;
  			$("#level").val(2);
  		}else if(string=='region'){
  			if($("#list_is_ring").val()==2){
		   		listType=4;
		   	}else{
			   	listType=3;
			}
			$("#level").val(2);
  		}else if(string=='ring'){
  			listType = 4;
  			$("#level").val(2);
  		}
  		else if(string=='region_ring'){
  			listType = 5;
  			$("#level").val(3);
  		}
  		clearList();
  		return;
  	} 
	
	switch(string){
		case "pro":
		$.ajax({
			type: "get",
		   	url: "prov_region_select.htm",
		   	cache: false,
		  	data: {provId:id},
		   	success: function(msg){
		  
		   		listType=1;
		   		$("#level").val(listType);
		   		clearList();
		   		
		   		if($(msg).find("name").text().length==3){
		   			var htmlText = "<select id='list_city' name='cityId' style='display:none'  />";
					$("#menuList").append(htmlText);
					var slt = $("#list_city");
					$('<option>',{val:$(msg).find("id").text(),text:$(msg).find("name").text()}).appendTo(slt);
		   			refreshArea('city');
		   			return;
		   		}
		   		
				if($(msg).find("content").length>0){			
					var htmlText = "<select id='list_city' name='cityId' onchange=refreshArea('city');></select>";
					$("#menuList").append(htmlText);
					var slt = $("#list_city");
					if($("input[@name='stypeId']:checked").val()!=20){
						$('<option>',{val:"0",text:"请选择",selected:"selected"}).appendTo(slt);
					}
					$(msg).find("content").each(function(i){
						var data = $(this); 
						$('<option>',{val:data.find("id").text(),text:data.find("name").text()}).appendTo(slt);
					});
				}
				
		   	}
		});
		break;
		
		case "city":
		if($("input[@name='stypeId']:checked").val()==20){
			break;
		}
		$.ajax({
			
		   	type: "get",
		   	url: "prov_region_select.htm",
		   	cache: false,
		   	data: {provId:id},
		   	success: function(msg){
		   		
			   	listType=2;
			   	$("#level").val(listType);
			   	clearList();
			   	if($(msg).find("cityRing").length>0){
			   		$("#cityId").val(id);
			   		var htmlText = "<select id='list_is_ring' name='isRing' onchange=refreshArea('is_ring');></select>";
					$("#menuList").append(htmlText);
					var slt = $("#list_is_ring");
					$('<option>',{val:"0",text:"请选择",selected:"selected"}).appendTo(slt);
					$('<option>',{val:"1",text:"按环形"}).appendTo(slt);
					$('<option>',{val:"2",text:"按区县"}).appendTo(slt);
			   	}else{
					if($(msg).find("content").length>0){	
						var htmlText = "<select id='list_region' name='regionId' onchange=refreshArea('region');></select>";
						$("#menuList").append(htmlText);
						var slt = $("#list_region");
						$('<option>',{val:"0",text:"请选择",selected:"selected"}).appendTo(slt);
						$(msg).find("content").each(function(i){
							var data = $(this); 
							$('<option>',{val:data.find("id").text(),text:data.find("name").text()}).appendTo(slt);
						});
					}
				}
		   	}
		});
		break;
		
		case "is_ring":
		$.ajax({
		   	type: "get",
		   	url: "prov_region_select.htm",
		   	cache: false,
		   	data: {provId:$("#cityId").val()},
		   	success: function(msg){
		   		
			   	listType=3;
			   	clearList();
			   	if(id==1){
			   		var htmlText = "<select id='list_ring' name='ringId' onchange=refreshArea('ring');></select>";
					$("#menuList").append(htmlText);
					var slt = $("#list_ring");
					$('<option>',{val:"0",text:"请选择",selected:"selected"}).appendTo(slt);
					$(msg).find("cityRing").each(function(i){
						var data = $(this); 
						$('<option>',{val:data.find("id").text(),text:data.find("name").text()}).appendTo(slt);
					});
			   	}else{
					if($(msg).find("content").length>0){			
						var htmlText = "<select id='list_region' name='regionId' onchange=refreshArea('region');></select>";
						$("#menuList").append(htmlText);
						var slt = $("#list_region");
						$('<option>',{val:"0",text:"请选择",selected:"selected"}).appendTo(slt);
						$(msg).find("content").each(function(i){
							var data = $(this); 
							$('<option>',{val:data.find("id").text(),text:data.find("name").text()}).appendTo(slt);
						});
					}
				}
		   	}
		});
		break;
		
		case "region":
		$.ajax({
		   	type: "get",
		   	url: "prov_region_select.htm",
		   	cache: false,
		   	data: {provId:id},
		   	success: function(msg){
		   		
		   		if($("#list_is_ring").val()==2){
		   			listType=4;
		   		}else{
			   		listType=3;
			   	}
			   	$("#level").val(3);
			   	clearList();
				if($(msg).find("content").length>0){			
					var htmlText = "<select id='list_region_ring' name='regionRingId' onchange=refreshArea('region_ring');></select>";
					$("#menuList").append(htmlText);
					var slt = $("#list_region_ring");
					$('<option>',{val:"0",text:"请选择",selected:"selected"}).appendTo(slt);
					$(msg).find("content").each(function(i){
						var data = $(this); 
						$('<option>',{val:data.find("id").text(),text:data.find("name").text()}).appendTo(slt);
					});
				}
		   	}
		});
		break;
		
		case "ring":
		$("#level").val(3);
		break;
		
		case "region_ring":
		$("#level").val(4);
		break;
	}
}

function setArea(){
	listType = 1;
	clearList();
	$("#list_pro option[value='0']").attr("selected","selected");
}